//
//  TTIsaacTextAlertView.m
//  TiantuLogistics
//
//  Created by Isaac on 16/9/20.
//  Copyright © 2016年 tiantu. All rights reserved.
//

#import "TTIsaacTextAlertView.h"

#define selfBacground [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0]


@implementation TTIsaacTextAlertView{
    // 标题
    UILabel * _title_label;
    // 内容
    UILabel * _body_label;
    UITextField *_textF;
}

-(instancetype)init{
    
    self=[super init];
    
    if (self) {
        
        self.frame=CGRectMake(0,SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_HEIGHT-64);
        //        self.backgroundColor = [UIColor blackColor];
    }
    
    return self;
}
//-(void)drawRect:(CGRect)rect{
//
//    if (self.title.length>0) {
//
//        _title_label.text=self.title;
//    }
//    if(self.body.length>0){
//        _body_label.text=self.body;
//    }
//}
-(void)createUI:(NSString *)cancel otherTitle:(NSString*)other{
    
    UIView *blackV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    blackV.backgroundColor = [UIColor blackColor];
    blackV.alpha = 0.3;
    [self addSubview:blackV];
    
    //    NSLog(@"cancel.length=%d",cancel.length);
    //
    //    NSLog(@"other.length=%d",other.length);
    
    UIView * white_view=[[UIView alloc]initWithFrame:XMRECT6(CGRectMake(50, 220,375-100, 160))];
    
    white_view.backgroundColor=[UIColor whiteColor];
    
    white_view.layer.cornerRadius=6;
    
    [self addSubview:white_view];
    
    //  title
    
    _title_label = [[UILabel alloc]initWithFrame:XMRECT6(CGRectMake(10,15,255, 30))];
    _title_label.textColor = kTextColorOne;
    _title_label.font = FONT15;
    _title_label.textAlignment = NSTextAlignmentCenter;
    [white_view addSubview:_title_label];
    
    
    UILabel * upfen_label=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_title_label.frame), 275*self6WidthRate, 1)];
    upfen_label.backgroundColor=selfBacground;
    [white_view addSubview:upfen_label];
    //  body
    
    _body_label=[[UILabel alloc]initWithFrame:XMRECT6(CGRectMake(30,70,100,20))];
    _body_label.font=[UIFont systemFontOfSize:14];
//    _body_label.textAlignment=NSTextAlignmentCenter;
    _body_label.textColor=kTextColorOne;
    //  换行最多两行
    //    _body_label.numberOfLines=2;
    [white_view addSubview:_body_label];
    
    _textF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_body_label.frame)+10, 50, 80, 30)];
    _textF.layer.borderWidth = 0.5;
    _textF.layer.borderColor = kTextColorThree.CGColor;
    _textF.layer.cornerRadius = 5;
    [_textF addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    _textF.keyboardType = UIKeyboardTypeDecimalPad;
    [white_view addSubview:_textF];
    _textF_text = _textF.text;
    
    
    UILabel * tipL=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_textF.frame)+5, 50, 24*self6WidthRate, 30)];
    tipL.text = @"箱";
    tipL.textColor = kTextColorTwo;
    tipL.font = FONT14;
//    tipL.backgroundColor=selfBacground;
    [white_view addSubview:tipL];
    
    //  分割线
    
    UILabel * fen_label=[[UILabel alloc]initWithFrame:CGRectMake(0, 120*self6HeightRate, 275*self6WidthRate, 1)];
    fen_label.backgroundColor=selfBacground;
    [white_view addSubview:fen_label];
    
    
    if (cancel.length&&other.length) {
        //  取消和确认按钮存在(两个按钮存在)
        
        for (int i=0; i<2;i++) {
            
            UIButton * button = [[UIButton alloc]initWithFrame:XMRECT6(CGRectMake((275/2)*i,120, (275/2), 40))];
            
            [white_view addSubview:button];
            
            UILabel * gray_line = [[UILabel alloc]initWithFrame:XMRECT6(CGRectMake((275/2), 120, 1, 40))];
            
            gray_line.backgroundColor = selfBacground;
            
            [white_view addSubview:gray_line];
            
            if (i==0) {
                
                [button setTitle:@"取消" forState:UIControlStateNormal];
                [button setTitleColor:kTextColorFour forState:UIControlStateNormal];
                [button addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
                
            }
            else if (i==1){
                
                [button setTitle:@"确定" forState:UIControlStateNormal];
                
                [button addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
                
                [button setTitleColor:kPriceColor forState:UIControlStateNormal];
                
            }
        }
    }else if (([cancel isEqual:nil]||[cancel isEqualToString:@""])&&([other isEqual:nil]||[other isEqualToString:@""])){
        
        // 取消按钮为空和确定按钮为空(无按钮情况)
        
        fen_label.hidden=YES;
        
        white_view.frame=XMRECT6(CGRectMake(50, 220,375-100, 150));
        
    }else if (cancel.length){
        
        //有一个确认按钮(一个按)没有网络请求相当于取消(虽是确认但是是取消）
        
        UIButton * button = [[UIButton alloc]initWithFrame:XMRECT6(CGRectMake(0,150,275, 40))];
        
        [white_view addSubview:button];
        
        UILabel * gray_line = [[UILabel alloc]initWithFrame:XMRECT6(CGRectMake((275/2), 150, 1, 40))];
        
        gray_line.backgroundColor = selfBacground;
        
        [white_view addSubview:gray_line];
        
        [button setTitle:@"确定" forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
        
        [button setTitleColor:kTextColorOne forState:UIControlStateNormal];
        
    }
    
}
//取消
-(void)cancel{
    
    [UIView animateWithDuration:0.3 animations:^{
        
        [self removeFromSuperview];
        
    } completion:^(BOOL finished) {
        
    }];
    
}
//确认
-(void)sure{
    
    if (_delegate &&[_delegate respondsToSelector:@selector(ClickedButtonAtIndex:)]) {
        
        [_delegate ClickedButtonAtIndex:1];
    }
}

-(void)showAlert{
    
    [UIView animateWithDuration:0.3 animations:^{
        
        UIWindow * window=[UIApplication sharedApplication].keyWindow;
        
        [window addSubview:self];
        
    } completion:^(BOOL finished) {
        
    }];
    
}

-(void)ISAACPopAletViewTitle:(NSString *)title tipMessage:(NSString *)message deleagte:(id)delegate cancelButton:(NSString *)cancel otherbuttonTitle:(NSString *)other{
    //    代理方法
    _delegate=delegate;
    //    创建UI
    [self createUI:cancel otherTitle:other];
    //    传值
    _body_label.text=message;
    
    _title_label.text=title;
}


- (void)textChange:(UITextField *)textF {
    _textF_text = textF.text;
}

@end
